/*
 * 问答、系统消息、私聊
 */

/*
 * ====================================================
 * 问答、系统消息、私聊
 */
$(function(){
	
});

function loadMsg(array){
	info("receive msg array:", array);
	if(!array || array.length==0)return;
	for(var i in array){
		var msg = array[i];
		if("publicMsg" == msg.type){
			addSysMsg(msg.content);
		}else{
			warn("the type of msg is unknown:", msg.type);
		}
	}
}

//装载一次问答数据：
//{inittimestamp:inittimestamp, qaArray：[{question:question,questionower:questionower,questiontimestamp:questiontimestamp,answer:answer,answertimestamp:answertimestamp}]}
function loadQA(data){
	info("[ipad-vod][loadQA]--------------------load qa data-----------------",data);
	var qadatas = data.qaArray;
	for(i in qadatas){
		var qa = qadatas[i];
		addQa(qa, "qaContainer");
	}
}

//加载一个系统消息
function addSysMsg(msg){
	info("[ipad-vod][addSysMsg] begin appending system message:", msg);
	var ele1 = createSysMsgEle(msg);
	$("#qaContainer").append(ele1);
}


//添加问答信息到某一列表（我的问答和所有问答列表）
function addQa(qa, parentId){
	var ele = createQaEle(qa);
	$("#"+parentId).append(ele);
	info("[ipad-vod][addQa] #"+parentId+":", ele);
}

//创建一个问答页面元素
function createQaEle(qa){
	if(typeof qa.questionowner!="string") qa.questionowner = "";
	if(typeof qa.answerowner!="string") qa.answerowner = "";
	var sEle = '<dl class="askList" name="'+qa.questionid+'">';
	sEle += '<dd class="q">'+ qa.question +'<p class="sign">- '+qa.questionowner;
	var qTime = showTime4Second(qa.questiontimestamp);
	if(qTime!=null) sEle += ' ('+qTime+')';
	sEle +='</p></dd>';
	if(typeof qa.answer == "string" && $.trim(qa.answer).length > 0){
		sEle += '<dd class="a">'+ qa.answer +'<p class="sign">- '+qa.answerowner;
		var aTime = showTime4Second(qa.answertimestamp);
		if(aTime!=null)sEle += ' ('+aTime+')';
		sEle +='</p></dd>';
	}
	sEle += '</dl>';
	return $(sEle);
}

function showTime4Second(sec){
	if(typeof sec != "number" && typeof sec != "string")return null;
	return showTime4Misec(Number(sec)*1000);
}

function showTime4Misec(misec){
	if(typeof misec != "number" && typeof misec != "string")return null;
	var d = new Date();
	d.setTime(Number(misec));
	return formatDate(d,"HH:mm:ss");
}

//创建一个系统消息页面元素
function createSysMsgEle(msg){
	return $('<dl class="systemSmg"><dt class="smg">'+iPadLang.systemMessagePrefix+'</dt><dd>'+ msg +'</dd></dl>');
}

